package pku.ss.movieSystem.common;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlTransient;

import daos.Movie;

public class PaginationSupport implements java.io.Serializable {

	private int pageSize = Constants.PAGE_SIZE;
	private List items = new ArrayList(0);
	private int totalCount;
	private int[] indexes = new int[0];
	private int startIndex = 0;
	private String jsonArray = "";
	
	public PaginationSupport() {}
	
	public PaginationSupport(List items, int totalCount) {
		setPageSize(Constants.PAGE_SIZE);
		setTotalCount(totalCount);
		setItems(items);
		setStartIndex(0);
	}
	
	public PaginationSupport(List items, int totalCount, int startIndex) {
		setPageSize(Constants.PAGE_SIZE);
		setTotalCount(totalCount);
		setItems(items);
		setStartIndex(startIndex);
	}
	
	public PaginationSupport(List items, int totalCount, int pageSize, int startIndex) {
		setPageSize(pageSize);
		setTotalCount(totalCount);
		setItems(items);
		setStartIndex(startIndex);
	}
	
	public List getItems() {
		return this.items;
	}
	
	@XmlTransient
	public void setItems(List items) {
		this.items = items;

		if(this.items.size()>0) {
			if(this.items.get(0) instanceof Movie) {
				setJsonArray(Utils.movieObjToJson((List<Movie>)this.items));
			}
		}
		else 
			setJsonArray("");
	}
	
	public int getPageSize() {
		return this.pageSize;
	}
	
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	public int getTotalCount() {
		return this.totalCount;
	}
	
	public void setTotalCount(int totalCount) {
		if(totalCount>0) {
			this.totalCount = totalCount;
			int count = totalCount/pageSize;
			if(totalCount%pageSize > 0) {
				count++;
			}
			
			indexes = new int[count];
			for(int i=0; i<count; i++) {
				indexes[i] = pageSize*i;
			}
		}
		else {
			this.totalCount = 0;
		}
	}
	
	public int[] getIndexes() {
		return indexes;
	}
	
	@XmlTransient
	public void setIndexes(int[] indexes) {
		this.indexes = indexes;
	}
	
	public int getStartIndex() {
		return this.startIndex;
	}
	
	public void setStartIndex(int startIndex) {
		if(totalCount <= 0) {
			this.startIndex = 0;
		}
		else if(startIndex >= totalCount) {
			this.startIndex = indexes[indexes.length-1];
		}
		else if(startIndex < 0) {
			this.startIndex = 0;
		}
		else {
			this.startIndex = indexes[startIndex/pageSize];
		}
	}
	
	public int getNextIndex() {
		int nextIndex = this.startIndex + pageSize;
		
		if(nextIndex >= totalCount)
			return this.startIndex;
		else
			return nextIndex;
	}
	
	public void setNextIndex(int nextIndex) {}
	
	public int getPreviousIndex() {
		int previousIndex = this.startIndex - pageSize;
		
		if(previousIndex < 0) 
			return 0;
		else
			return previousIndex;
		
	}
	
	public void setPreviousIndex(int previousIndex) {}
	
	public String getJsonArray() {
		return this.jsonArray;
	}
	
	public void setJsonArray(String jsonArray) {
		this.jsonArray = jsonArray;
	}
}
